package
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	
	/**
	 * ...
	 * @author Danilo Gama
	 */
	public class Heroi extends MovieClip
	{
		
		var up:Boolean;
		var down:Boolean;
		var left:Boolean;
		var right:Boolean;
		
	
		public function Heroi()
		{
			Main.stage.addEventListener(KeyboardEvent.KEY_DOWN, Press);
			Main.stage.addEventListener(KeyboardEvent.KEY_UP, Release);
		
		}
		
		public function Press(e:KeyboardEvent)
		{
			if (e.keyCode == Keyboard.UP)
			{
				up = true;
				
			}
			if (e.keyCode == Keyboard.DOWN)
			{
				down = true;
			}
			if (e.keyCode == Keyboard.LEFT)
			{
				left = true;
				this.gotoAndStop("esquerda");
			}
			if (e.keyCode == Keyboard.RIGHT)
			{
				right = true;
				this.gotoAndStop("direita");
			}
			if (e.keyCode == Keyboard.SPACE)
			{
				var tiro : TiroHeroi = new TiroHeroi();
				tiro.x = this.x;
				tiro.y = this.y;
				Main.screen.addChild(tiro);
				Level.AR_tiros.push(tiro);
				trace(tiro.x , tiro.y , this.x , this.y)
			}
		}
		
		public function Release(e:KeyboardEvent)
		{
			if (e.keyCode == Keyboard.UP)
			{
				up = false;
			}
			if (e.keyCode == Keyboard.DOWN)
			{
				down = false;
			}
			if (e.keyCode == Keyboard.LEFT)
			{
				left = false;
				this.gotoAndStop("normal")
			}
			if (e.keyCode == Keyboard.RIGHT)
			{
				right = false;
				this.gotoAndStop("normal");
			}
		}
		
		public function update():void
		{
			trace("update do heroi")
			if (up)
			{
				this.y -= 5;
			}
			else if (down)
			{
				this.y += 5;
			}
			if (left)
			{
				this.x -= 5;
			}
			else if (right)
			{
				this.x += 5;
			}
			
			if (this.x >= Main.stage.stageWidth - this.width/2)
			{
				this.x = Main.stage.stageWidth - this.width/2;
			}
			if (this.x <= 0 + this.width/2)
			{
				this.x = 0 + this.width/2;
			}
			if (this.y >= Main.stage.stageHeight)
			{
				this.y = Main.stage.stageHeight;
			}
			if (this.y <= 200)
			{
				this.y = 200;
			}
			
		
		}
	
	}

}